package cn.fintecher.pangolin.service.business.web;

import cn.fintecher.pangolin.common.web.BaseController;
import cn.fintecher.pangolin.service.business.model.CardInfoModel;
import cn.fintecher.pangolin.service.business.model.request.CollectingCasesRequest;
import cn.fintecher.pangolin.service.business.model.request.FindRetireCaseRequest;
import cn.fintecher.pangolin.service.business.model.request.ModifyCommentRequest;
import cn.fintecher.pangolin.service.business.model.request.UpdateStatusRequest;
import cn.fintecher.pangolin.service.business.model.response.*;
import cn.fintecher.pangolin.service.business.service.CaseCollectorService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Set;

/**
 * @Auther: xiaqun
 * @Description: 催收员案件操作相关接口
 * @Date: 14:29 2019/9/3
 */

@RestController
@RequestMapping("/api/caseCollectorController")
@Api(value = "催收员案件操作相关接口", description = "催收员案件操作相关接口")
public class CaseCollectorController extends BaseController {
    @Autowired
    private CaseCollectorService caseCollectorService;

    /**
     * 查询登录人相关的委托方
     */
    @ApiOperation(value = "查询登录人相关的委托方", notes = "查询登录人相关的委托方")
    @GetMapping("/findPrincipal")
    public ResponseEntity<Set<PrincipalResponse>> findPrincipal(@RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request find principal");
        Set<PrincipalResponse> set = caseCollectorService.findPrincipal(token);
        return ResponseEntity.ok().body(set);
    }

    /**
     * 在催案件查询
     */
    @ApiOperation(value = "在催案件查询", notes = "在催案件查询")
    @GetMapping("/findAllInCollectCase")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<CollectingCasesResponse>> findAllInCollectCase(CollectingCasesRequest request, Pageable pageable,
                                                                              @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to find all in collect case,{}", request);
        Page<CollectingCasesResponse> page = caseCollectorService.findAllInCollectCase(request, pageable, token);
        return ResponseEntity.ok().body(page);
    }

    /**
     * 查询委托方案件
     */
    @ApiOperation(value = "查询委托方案件", notes = "查询委托方案件")
    @GetMapping("/findAllCaseByPrincipal")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<CollectingCasesResponse>> findAllCaseByPrincipal(CollectingCasesRequest request, Pageable pageable,
                                                                                @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to find all case by principal,{}", request);
        Page<CollectingCasesResponse> page = caseCollectorService.findAllCaseByPrincipal(request, pageable, token);
        return ResponseEntity.ok().body(page);
    }

    /**
     * 退案案件查询
     */
    @ApiOperation(value = "退案案件查询", notes = "退案案件查询")
    @GetMapping("/findAllRetireCase")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<FindRetireCaseResponse>> findAllRetireCase(FindRetireCaseRequest request, Pageable pageable) {
        log.info("REST request to find retire case,{}", request);
        Page<FindRetireCaseResponse> page = caseCollectorService.findAllRetireCase(request, pageable);
        return ResponseEntity.ok().body(page);
    }

    /**
     * 查询委托方下全部批次案件
     */
    @ApiOperation(value = "查询委托方下全部批次案件", notes = "查询委托方下全部批次案件")
    @GetMapping("/findAllBatchCase")
    public ResponseEntity<List<FindBatchCaseResponse>> findAllBatchCase(@RequestParam(required = false) @ApiParam(value = "批次号") String batchNumber,
                                                                        @RequestParam @ApiParam(value = "委托方ID", required = true) String principalId,
                                                                        @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to find all batch case,{},{}", batchNumber, principalId);
        List<FindBatchCaseResponse> list = caseCollectorService.findAllBatchCase(batchNumber, principalId, token);
        return ResponseEntity.ok().body(list);
    }

    /**
     * 查询委托方下全部案件
     */
    @ApiOperation(value = "查询委托方下全部案件", notes = "查询委托方下全部案件")
    @GetMapping("/findAllCase")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<FindCaseResponse>> findAllCase(CollectingCasesRequest request, Pageable pageable,
                                                              @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to find case response,{}", request);
        Page<FindCaseResponse> page = caseCollectorService.findAllCase(request, pageable, token);
        return ResponseEntity.ok().body(page);
    }

    /**
     * 根据案件ID获取案件
     */
    @ApiOperation(value = "根据案件ID获取案件", notes = "根据案件ID获取案件")
    @GetMapping("/getCaseInfoById")
    public ResponseEntity<CaseInfoModelResponse> getCaseInfoById(@RequestParam @ApiParam(value = "案件ID", required = true) String caseId) {
        log.info("REST request to get case info by id,{}", caseId);
        CaseInfoModelResponse response = caseCollectorService.getCaseInfoById(caseId);
        return ResponseEntity.ok().body(response);
    }

    /**
     * 获取客户账号卡号
     */
    @ApiOperation(value = "获取客户账号卡号", notes = "获取客户账号卡号")
    @GetMapping("/getCardInfoByIdCard")
    public ResponseEntity<List<CardInfoModel>> getCardInfoByIdCard(@RequestParam @ApiParam(value = "证件号", required = true) String certificateNo,
                                                                   @RequestParam @ApiParam(value = "案件ID", required = true) String caseId,
                                                                   @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to get card info by id,{},{}", certificateNo, caseId);
        List<CardInfoModel> list = caseCollectorService.getCardInfoByIdCard(certificateNo, caseId, token);
        return ResponseEntity.ok().body(list);
    }

    /**
     * 查询案件记事本内容
     */
    @ApiOperation(value = "查询案件记事本内容", notes = "查询案件记事本内容")
    @GetMapping("/getComment")
    public ResponseEntity<String> getComment(@RequestParam @ApiParam(value = "案件ID", required = true) String caseId) {
        log.info("REST request to get comment,{}", caseId);
        String comment = caseCollectorService.getComment(caseId);
        return ResponseEntity.ok().body(comment);
    }

    /**
     * 修改记事本内容
     */
    @ApiOperation(value = "修改记事本内容", notes = "修改记事本内容")
    @PutMapping("/modifyComment")
    public ResponseEntity<Void> modifyComment(@RequestBody ModifyCommentRequest request) {
        log.info("REST request modify comment,{}", request);
        caseCollectorService.modifyComment(request);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询案件状态
     */
    @ApiOperation(value = "查询案件状态", notes = "查询案件状态")
    @GetMapping("/getCaseStatus")
    public ResponseEntity<CaseStatusResponse> getCaseStatus(@RequestParam @ApiParam(value = "案件ID", required = true) String caseId) {
        log.info("REST request get case status,{}", caseId);
        CaseStatusResponse response = caseCollectorService.getCaseStatus(caseId);
        return ResponseEntity.ok().body(response);
    }

    /**
     * 更新案件状态
     */
    @ApiOperation(value = "更新案件状态", notes = "更新案件状态")
    @PutMapping("/updateStatus")
    public ResponseEntity<Void> updateStatus(@Valid @RequestBody UpdateStatusRequest request) {
        log.info("REST request update status,{}", request);
        caseCollectorService.updateStatus(request);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询委托方下批次号
     */
    @ApiOperation(value = "查询委托方下批次号", notes = "查询委托方下批次号")
    @GetMapping("/getBatchNumber")
    public ResponseEntity<List<FindBatchCaseResponse>> getBatchNumber(@RequestParam @ApiParam(value = "委托方ID", required = true) String principalId,
                                                                      @RequestParam @ApiParam(value = "案件池类型", required = true) String type) {
        log.info("REST request to get batch number,{}", principalId);
        List<FindBatchCaseResponse> list = caseCollectorService.getBatchNumber(principalId, type);
        return ResponseEntity.ok().body(list);
    }
}